<?php
echo '<script type="text/javascript">';
require APP . WEBROOT_DIR . DS . "js" . DS . "validate" . DS . "theater_add.js";
echo '</script>';
?>
<div class="theaters form">
    <?php echo $this->Form->create('Theater', array('id' => 'TheaterAddForm')); ?>
    <fieldset>
        <legend><?php __('Editar Obra de Teatro'); ?></legend>
        <?php
        echo $this->Form->input('id');
        echo $this->Form->input('name', array('label' => 'Nombre'));
        echo $this->Form->input('contact_name', array('label' => 'Nombre de Contacto'));
        echo $this->Form->input('address', array('label' => 'Dirección'));
        echo $this->Form->input('phone_number', array('label' => 'Teléfono Fijo'));
        echo $this->Form->input('movil_number', array('label' => 'Teléfono Celular'));
        echo $this->Form->input('email', array('label' => 'Email'));
        ?>
        <div class="time_range">
            <?php
            echo $this->Form->input('date_from', array('label' => 'Fecha de Inicio', 'div' => array('class' => 'input text required fecha')));
            ?>
            <div class="left">
                <label>Hora de Inicio</label>
                <?php
                echo $this->Form->input('start_hour', array('label' => false, 'type' => 'select', 'options' => $hours));
                ?>
                <span>:</span>
                <?php
                echo $this->Form->input('start_minute', array('label' => false, 'type' => 'select', 'options' => $minutes));
                ?>
            </div>
            <div class="left">
                <label>Hora de Fin</label>
                <?php
                echo $this->Form->input('finish_hour', array('label' => false, 'type' => 'select', 'options' => $hours));
                ?>
                <span>:</span>
                <?php
                echo $this->Form->input('finish_minute', array('label' => false, 'type' => 'select', 'options' => $minutes));
                ?>
            </div>
        </div>
        <?php
        echo $this->Form->input('description', array('label' => 'Descripción', 'type' => 'textarea'));
        echo $this->Form->input('ticket_price', array('label' => 'Precio Entrada'));
        echo $this->Form->input('ticket_sales', array('label' => 'Cantidad de Entradas Vendidas'));
        ?>
        <fieldset>
            <legend><?php __('Trajes'); ?></legend>
            <?php
            foreach ($costumes as $costume_id => $costume_name) {
                echo $this->Form->input('TheatersCostume.costume_id' . $costume_id, array('label' => $costume_name,
                'type' => 'checkbox',
                'name' => 'data[TheatersCostume][][costume_id]',
                'value' => $costume_id,
                'hiddenField' => false,
                'checked' => isset($theaters_costumes[$costume_id]) ? 'checked' : ''));
            }
            ?>
        </fieldset>
        <fieldset>
            <legend><?php __('Empleados'); ?></legend>
            <?php
            $cant_empleados = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10);
            echo $this->Form->input('employee_total', array('label' => 'Cantidad', 'value' => count($cant_empleados), 'type' => 'hidden'));
            echo $this->Form->input('employee_count', array('label' => 'Cantidad', 'type' => 'select', 'options' => $cant_empleados, 'onchange' => 'mostrar_empleados(); return false;'));
            for ($i = 1; $i <= count($cant_empleados); $i++) {
                ?>
            <div id="empleado_<?php echo $i; ?>" class="employee_cost">
                    <?php
                    echo $this->Form->input('employee_id_' . $i, array('label' => 'Nombre y Apellido', 'type' => 'select', 'options' => $employees, 'empty' => 'Seleccione', 'name' => 'data[TheatersEmployee][' . $i . '][employee_id]', 'value'=>isset($this->data['TheatersEmployee'][$i])?$this->data['TheatersEmployee'][$i]['employee_id']:''));
                    echo $this->Form->input('employee_cost_' . $i, array('label' => 'Costo', 'name' => 'data[TheatersEmployee][' . $i . '][cost]', 'value'=>isset($this->data['TheatersEmployee'][$i])?$this->data['TheatersEmployee'][$i]['cost']:''));
                    echo $this->Form->input('employee_paid_' . $i, array('type'=>'hidden', 'name' => 'data[TheatersEmployee][' . $i . '][paid]', 'value'=>isset($this->data['TheatersEmployee'][$i])?$this->data['TheatersEmployee'][$i]['paid']:0));
                    ?>
            </div>
                <?php
            }
            ?>
        </fieldset>
        <fieldset>
            <legend><?php __('Gastos Extras'); ?></legend>
            <?php
            $cant_gastos = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11=>11, 12=>12, 13=>13, 14=>14, 15=>15, 16=>16, 17=>17, 18=>18, 19=>19, 20=>20);
            echo $this->Form->input('expense_total', array('label' => 'Cantidad', 'value' => count($cant_gastos), 'type' => 'hidden'));
            echo $this->Form->input('expense_count', array('label' => 'Cantidad', 'type' => 'select', 'options' => $cant_gastos, 'empty'=>'Ninguno','onchange'=>'mostrar_gastos(); return false;'));
            for ($i = 1; $i <= count($cant_gastos); $i++) {
                ?>
            <div id="gasto_<?php echo $i; ?>" class="employee_cost">
                    <?php
                    echo $this->Form->input('expense_description_' . $i, array('label' => 'Descripción', 'name'=>'data[TheatersExpense]['.$i.'][description]', 'value'=>isset($this->data['TheatersExpense'][$i])?$this->data['TheatersExpense'][$i]['description']:''));
                    echo $this->Form->input('expense_cost_' . $i, array('label' => 'costo', 'name'=>'data[TheatersExpense]['.$i.'][cost]', 'value'=>isset($this->data['TheatersExpense'][$i])?$this->data['TheatersExpense'][$i]['cost']:''));
                    ?>
            </div>
                <?php
            }
            ?>
        </fieldset>
    </fieldset>
    <?php echo $this->Form->end(__('Editar', true)); ?>
</div>
<script type="text/javascript" defer>
    (function($){
        // Date picker
        $("#TheaterDateFrom").datepicker({
            //yearRange: '1970:2010',
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });
        // Date picker
        $("#TheaterDateTo").datepicker({
            //yearRange: '1970:2010',
            defaultDate: new Date(),
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true
        });

        mostrar_empleados();
        mostrar_gastos();

    })(jQuery);

    function mostrar_empleados(){
        //leo la cantidad de usuarios que tiene seteado y solo muestro esa cantidad
        //el resto se los oculto
        var total_empleados = $('#TheaterEmployeeTotal').val();
        var cant_empleados = $('#TheaterEmployeeCount').val();
        var i = 1;
        for(i; i <= total_empleados; i++){
            if(i <= cant_empleados){
                $('#empleado_'+i).show();
                //agrego las clases de validacion
                $('#TheaterEmployeeId'+i).addClass('validEmployeeId');
                $('#TheaterEmployeeCost'+i).addClass('validEmployeeCost');
            }else{
                $('#empleado_'+i).hide();
                $('#TheaterEmployeeId'+i).val('');
                $('#TheaterEmployeeCost'+i).val('');
                //le saco las clases de validacion
                $('#TheaterEmployeeId'+i).removeClass('validEmployeeId');
                $('#TheaterEmployeeCost'+i).removeClass('validEmployeeCost');
            }
        }
    }

    function mostrar_gastos(){
        //leo la cantidad de usuarios que tiene seteado y solo muestro esa cantidad
        //el resto se los oculto
        var total_gastos = $('#TheaterExpenseTotal').val();
        var cant_gastos = $('#TheaterExpenseCount').val();
        var i = 1;
        for(i; i <= total_gastos; i++){
            if(i <= cant_gastos){
                $('#gasto_'+i).show();
                //agrego las clases de validacion
                $('#TheaterExpenseDescription'+i).addClass('validExpenseDescription');
                $('#TheaterExpenseCost'+i).addClass('validExpenseCost');
            }else{
                $('#gasto_'+i).hide();
                $('#TheaterExpenseCost'+i).val('');
                $('#TheaterExpenseDescription'+i).val('');
                //le saco las clases de validacion
                $('#TheaterExpenseDescription'+i).removeClass('validExpenseDescription');
                $('#TheaterExpenseCost'+i).removeClass('validExpenseCost');
            }
        }
    }
</script>